package com.dl7.mvp.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import com.dl7.downloaderlib.entity.FileInfo;
import com.dl7.mvp.R;
import com.dl7.mvp.engine.DownloaderWrapper;
import com.dl7.mvp.local.table.VideoInfo;
import com.dl7.mvp.module.video.player.VideoPlayerActivity;
import com.dl7.mvp.utils.DefIconFactory;
import com.dl7.mvp.utils.ImageLoader;
import com.dl7.mvp.utils.StringUtils;
import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.adapter.BaseViewHolder;

/**
 * Created by long on 2016/12/19.
 * video列表支持点击下载， 并且显示下载进度条的适配器基类
 */
public class BaseVideoListAdapter extends BaseQuickAdapter<VideoInfo> {
    public BaseVideoListAdapter(Context context) {
        super(context);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.adapter_video_list;
    }

    @Override
    protected void convert(final BaseViewHolder holder, final VideoInfo item) {
        setTag(holder, item.getUrl());
        final ImageView ivPhoto = holder.getView(R.id.iv_photo);
        ImageLoader.loadFitCenter(mContext, item.getImage(), ivPhoto, DefIconFactory.provideIcon());
        holder.setText(R.id.tv_title, item.getName());
        holder.getConvertView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                VideoPlayerActivity.launch(mContext, item);
            }
        });
        holder.getView(R.id.iv_video_download).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DownloaderWrapper.start(item);
                holder.setVisible(R.id.iv_video_download, false);
            }
        });
        _switchViews(holder, item);
    }

    /**
     * 更新视图
     *
     * @param holder
     * @param item
     */
    private void _switchViews(BaseViewHolder holder, VideoInfo item) {
//        switch (item.getDownloadStatus()) {
//            case DownloadStatus.COMPLETE:
//                holder.setVisible(R.id.iv_video_download, true);
//                holder.setImageResource(R.id.iv_video_download, R.mipmap.ic_photo_downloaded);
//                break;
//            case DownloadStatus.DOWNLOADING:
//                holder.setVisible(R.id.iv_video_download, false);
//                NumberProgressBar pbDownload = holder.getView(R.id.pb_download);
//                holder.setVisible(R.id.pb_download, true);
//                pbDownload.setMax((int) item.getTotalSize());
//                pbDownload.setProgress((int) item.getLoadedSize());
//                break;
//            default:
//                holder.setVisible(R.id.iv_video_download, true);
//                holder.setImageResource(R.id.iv_video_download, R.mipmap.ic_photo_download);
//                break;
//        }
    }

    /*********************************************************************/
    /**
     * 更新下载状态
     *
     * @param info 应用信息
     */
    private void _updateDownload(VideoInfo info) {
        BaseViewHolder holder = getTag(info.getUrl());
        if (holder == null) {
            return;
        }
        _switchViews(holder, info);
    }

    /**
     * 查找 VideoInfo
     * url:http://flv2.bn.netease.com/videolib3/1501/28/wlncJ2098/SD/wlncJ2098-mobile.mp4
     *
     * @param name wlncJ2098-mobile.mp4 -> 文件名
     * @return
     */
    private VideoInfo _find(String name) {
        for (VideoInfo info : mData) {
            if (name.equals(StringUtils.clipFileName(info.getUrl()))) {
                return info;
            }
        }
        return null;
    }

    /**
     * 更新下载状态
     *
     * @param fileInfo 文件信息
     */
    public void updateDownload(FileInfo fileInfo) {
        VideoInfo info = _find(fileInfo.getName());
        if (info == null) {
            return;
        }
        info.setDownloadStatus(fileInfo.getStatus());
        info.setTotalSize(fileInfo.getTotalBytes());
        info.setLoadedSize(fileInfo.getLoadBytes());
        info.setDownloadSpeed(fileInfo.getSpeed());

        _updateDownload(info);
    }
}
